#!/usr/bin/env bash

set -e -u -x
source ./tasks/helpers.sh

task_working_dir

export PATH="$STAGE_EMBEDDED_PATH"
export BUNDLE_GEMFILE="$SOURCE_DIR/build/package/Gemfile"
BUNDLE_APP_CONFIG="$(pwd)/_persist/.bundle"
export BUNDLE_APP_CONFIG

(
  # Install into the task's special _persist directory so the installed gems
  # dir isn't cleaned between runs for development purposes.
  mkdir -p _persist
  cd _persist

  # Install all the gems for use in packaging.
  perl -0 -p -i -e 's/BUNDLED WITH[\n\r]+.+$//igs' Gemfile.lock
  bundle config --local build.nokogiri --use-system-libraries
  bundle config --local path "$(pwd)/bundle"
  bundle config --local clean true
  bundle install
)

work_destdir="$(pwd)/destdir"
rm -rf "$work_destdir"
(
  cd "$BUILD_DIR"
  make
  make install DESTDIR="$work_destdir"
)
PACKAGE=core WORK_DESTDIR="$work_destdir" PACKAGE_WORK_DIR="$PACKAGE_WORK_DIR" "$SOURCE_DIR/build/package/build_package"
rm -rf "$work_destdir"
